//
//  CSTransferPayController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/9/19.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHBalanceModelView.h"
#import "SHTextFieldCell.h"
#import "CSTransferDetailController.h"
#import "CSTransferPayController.h"

@interface CSTransferPayController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *transferPayTable;
@property(nonatomic,strong)SHTextFieldCell *amountCell;//转账金额
@property(nonatomic,strong)SHTextFieldCell *nameCell;//姓名
@property(nonatomic,strong)SHTextFieldCell *laterFiveCardNoCell;//卡号后5位
@property(nonatomic,strong)UITableViewCell *bottomCell;

@end

@implementation CSTransferPayController

- (UITableView *)transferPayTable{
    
    if (!_transferPayTable) {
        
        _transferPayTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _transferPayTable.scrollEnabled = NO;
        _transferPayTable.estimatedRowHeight = 100;
        _transferPayTable.rowHeight = UITableViewAutomaticDimension;
        _transferPayTable.delegate = self;
        _transferPayTable.dataSource = self;
        _transferPayTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_transferPayTable];
    }
    
    
    return _transferPayTable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"VIP转账";
    self.view.backgroundColor = [UIColor whiteColor];
    [self transferPayTable];
    
    SHButton *confirmBtn = [SHButton buttonWithType:UIButtonTypeCustom title:@"确认转账" font:16 titleColor:[UIColor whiteColor] textAlignment:0 forState:UIControlStateNormal];
    confirmBtn.backgroundColor = KREDCOLOR;
    confirmBtn.layer.cornerRadius = __I6Width(5);
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(confirmBtncliked:) forControlEvents:UIControlEventTouchUpInside];
    [self.transferPayTable addSubview:confirmBtn];
    
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).with.offset(__I6Height(230));
        make.centerX.mas_equalTo(self.view.mas_centerX).with.offset(0);
        make.width.equalTo(@(__I6Width(326)));
        make.height.equalTo(@(__I6Height(42)));
        
        
    }];
    

    // Do any additional setup after loading the view.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return __I6Height(15);
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(16))];
    headerView.backgroundColor = KBASEBGCOLOR;
    
    return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    if (indexPath.row == 0) {
        _amountCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        _amountCell.labelText  = @"转账金额";
//        _amountCell.placeholder = @"请输入整数";
        _amountCell.contentTf.text = self.amount;
        _amountCell.contentTf.clearButtonMode = UITextFieldViewModeNever;
        _amountCell.contentTf.userInteractionEnabled = NO;
        return _amountCell;
    }else if (indexPath.row == 1){
        
        _nameCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        _nameCell.labelText  = @"汇款人姓名";
        _nameCell.placeholder = @"请输入汇款人姓名";
        return _nameCell;

        
    }else if(indexPath.row == 2){
        
        _laterFiveCardNoCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        _laterFiveCardNoCell.labelText  = @"汇款账号后5位";
        _laterFiveCardNoCell.placeholder = @"请输入汇款账号后5位数字";
        return _laterFiveCardNoCell;
        

        
    }else{
        
       _bottomCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        _bottomCell.selectionStyle = UITableViewCellSelectionStyleNone;
        SHLabel *promoteLabel = [SHLabel labelWithText:@"说明:每一笔转账金额在2万至5万之间,汇款账号请正确填写否则不能转账" font:__I6Width(15) textColor:KGARACOLOR textAlignment:NSTextAlignmentLeft];
        promoteLabel.numberOfLines = 0;
        
        [_bottomCell addSubview:promoteLabel];
        [promoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_bottomCell.mas_top).with.offset(__I6Height(20));
            make.left.mas_equalTo(_bottomCell.mas_left).with.offset(__I6Width(15));
            make.right.mas_equalTo(_bottomCell.mas_right).with.offset(__I6Width(-15));

            
        }];
        
        
        

        return _bottomCell;
        
        
    }
}


#pragma mark - 确认转账
- (void)confirmBtncliked:(UIButton *)seder{
    [self.view endEditing:YES];
    NSNumber *amomunt  = [NSNumber numberWithInteger:[_amountCell.contentTf.text integerValue]];
    NSString *bank_tail  = _laterFiveCardNoCell.contentTf.text;
    NSString *remitter  = _nameCell.contentTf.text;

    if (remitter.length == 0) {
        
        [self.view makeToast:@"请填写汇款人姓名" duration:2.f position:@"center"];
        return;
    }
    
    if ((bank_tail.length == 0) || (bank_tail.length != 5 )) {
        
        [self.view makeToast:@"请填写汇款账号后5位数字" duration:2.f position:@"center"];

        return;
    }
    
    
    NSDictionary *params = @{@"amount":amomunt,@"bank_tail":bank_tail,@"remitter":remitter};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
    [MyDataService requestAFURL:@"/balanceLucky/api/transferinto_cl_json" httpMethod:@"POST" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([result[@"success"] boolValue]) {
            
            CSTransferDetailController *transferDetailVc = [[CSTransferDetailController alloc]init];
            if (result[@"data"]) {
                 transferDetailVc.orderId = result[@"data"][@"orderId"];
                transferDetailVc.orderCode = result[@"data"][@"orderCode"];
                transferDetailVc.laterFiveCode = _laterFiveCardNoCell.contentTf.text;
                transferDetailVc.amount = _amountCell.contentTf.text;
            }
           
            [self.navigationController pushViewController:transferDetailVc animated:YES];
            
        }else{
             [self.view makeToast:result[@"data"] duration:2 position:@"center"];
        }
       
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        
    }];
   
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
